//
//  CJRegular.m
//  TMSDriver
//
//  Created by zcj on 2018/4/10.
//  Copyright © 2018年 zhuchongjin All rights reserved.
//

#import "CJRegular.h"

@implementation CJRegular


/// 正则校验
/// @param strRegex 正则表达式
/// @param strContent 校验内容
+ (BOOL)getResultByRegex:(NSString *)strRegex content:(NSString *)strContent{
    
    strContent = [strContent stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",strRegex];
    return [emailTest evaluateWithObject:strContent];
}

/**
 用户名字   纯汉字
 
 @param name <#name description#>
 @return <#return value description#>
 */
+ (BOOL)getvalidateUserName:(NSString *)name{

    NSString *userNameRegex = @"^[\u4e00-\u9fa5]{2,6}+$";
    return [CJRegular getResultByRegex:userNameRegex content:name];
}


/// 密码校验 8位及以上非空格字符组成的字符串，数字，字母，特殊字符至少有 两种
/// @param passWord <#passWord description#>
+ (BOOL)getvalidatePassWord:(NSString *)passWord{
    
    NSString *userNameRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)(?![_`~!@#$%^&*.]+$)[0-9A-Za-z_`~!@#$%^&*.]{8,20}$";
    return [CJRegular getResultByRegex:userNameRegex content:passWord];
}

/**
 手机号验证  11位
 
 @param phoneNumber <#phoneNumber description#>
 @return <#return value description#>
 */
+ (BOOL)getcheckPhone:(NSString *)phoneNumber

{
    phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (phoneNumber.length != 11) {
        return NO;
    }
    
//    NSString *regex = @"^((19[0-9])|(13[0-9])|(147)|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$";
//
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
//
//    BOOL isMatch = [pred evaluateWithObject:phoneNumber];
//
//    if (!isMatch)
//    {
//
//        return NO;
//
//    }
    return YES;
    
}

/**
 邮箱验证
 
 @param Email <#Email description#>
 @return <#return value description#>
 */
+ (BOOL)getIsEmailAdress:(NSString *)Email
{
    NSString *emailCheck = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    return [CJRegular getResultByRegex:emailCheck content:Email];
}


/**
 验证QQ号长度 4-12 位
 
 @param strQQ <#strQQ description#>
 @return <#return value description#>
 */
+ (BOOL)getQQNum:(NSString *)strQQ{

    NSString *strRegex = @"[1-9][0-9]{4,12}";
    return [CJRegular getResultByRegex:strRegex content:strQQ];
}


/**
 手机号验证码   6 位纯数字
 
 @param auth <#auth description#>
 @return <#return value description#>
 */
+ (BOOL)getPhoneNumAouth:(NSString *)auth{
    
    NSString *strRegex = @"[0-9]{6}";
    return [CJRegular getResultByRegex:strRegex content:auth];

}


/**
 实名付 验证码 6位纯数字
 
 @param auth <#auth description#>
 @return <#return value description#>
 */
+ (BOOL)getSMFAouth:(NSString *)auth{
    
    NSString *strRegex = @"[0-9]{6}";
    return [CJRegular getResultByRegex:strRegex content:auth];
}



/**
 
 交易密码 设置  6位数字
 @param auth <#str description#>
 @return <#return value description#>
 */
+ (BOOL)getDealCode:(NSString *)auth{
    
    NSString *strRegex = @"[0-9]{6}";
    return [CJRegular getResultByRegex:strRegex content:auth];

}


/// 检查真实日期 年月日
/// @param str <#str description#>
+ (BOOL)getCheckRealDataTimeYMD:(NSString *)str{

    NSString *strRegex = @"^([1-9][0-9]{3})(-)([0][1-9]|[1][0|1|2])(-)([0][1-9]|[1|2][0-9]|[3][0|1])$";
    return [CJRegular getResultByRegex:strRegex content:str];
}


/**
 身份证验证
 
 @param Identity <#Identity description#>
 @return <#return value description#>
 */
+ (BOOL)getVerifyIdentityCardNum:(NSString *)Identity{
    
    NSString *userID = [Identity stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    //长度不为18的都排除掉
    if (userID.length != 18) {

        return NO;
    }

    //校验格式

    NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";

    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];

    BOOL flag = [identityCardPredicate evaluateWithObject:userID];

    if (!flag) {
        return flag; //格式错误
    }else{

        //格式正确在判断是否合法

        //将前17位加权因子保存在数组里
        NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];

        //这是除以11后，可能产生的11位余数、验证码，也保存成数组

        NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];

        //用来保存前17位各自乖以加权因子后的总和

        NSInteger idCardWiSum = 0;

        for(int i = 0;i < 17;i++){

        NSInteger subStrIndex = [[userID substringWithRange:NSMakeRange(i, 1)] integerValue];

        NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];

        idCardWiSum+= subStrIndex * idCardWiIndex;

        }

        //计算出校验码所在数组的位置
        NSInteger idCardMod=idCardWiSum%11;

        //得到最后一位身份证号码
        NSString * idCardLast= [userID substringWithRange:NSMakeRange(17, 1)];

        //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
        if(idCardMod==2){

            if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]){
                return YES;

            }else{

                return NO;
            }

        }else{

            //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
            if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]]){

                return YES;

            }else{

                return NO;

            }

        }

    }

    
//    NSString *strRegex = @"^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$";
//    return [CJRegular getResultByRegex:strRegex content:Identity];

}


/**
 登陆  密码 数字 字母大小写 下划线 6-20位
 
 @param str <#str description#>
 @return <#return value description#>
 */
+ (BOOL)getLogInPassWord:(NSString *)str{
    
    NSString *strRegex = @"[a-zA-Z0-9_]{6,20}";
    return [CJRegular getResultByRegex:strRegex content:str];
}


/// 货主 名称简码 纯字母 2-4位
/// @param str <#str description#>
+ (BOOL)getPrefixVerification:(NSString *)str{
    
    NSString *strRegex = @"[a-zA-Z]{2,4}";
    return [CJRegular getResultByRegex:strRegex content:str];
}


/**
 昵称 4-20位 汉字大小字母 下划线
 
 @param str <#str description#>
 @return <#return value description#>
 */
+ (BOOL)getNickName:(NSString *)str{
    
    NSString *strRegex = @"[\u4e00-\u9fa5_a-zA-Z0-9_]{2,20}";
    return [CJRegular getResultByRegex:strRegex content:str];
}

/**
 图片验证  4 位数字大小写英文
 
 @param str <#str description#>
 @return <#return value description#>
 */
+ (BOOL)getPicAuth:(NSString *)str{
    
    NSString *strRegex = @"[a-zA-Z0-9]{4}";
    return [CJRegular getResultByRegex:strRegex content:str];
}


/**
 银行卡验证
 
 @param cardNumber <#cardNumber description#>
 @return <#return value description#>
 */
+(BOOL)getIsBankCardNum:(NSString *)cardNumber{
    if(cardNumber.length==0){
        return NO;
    }
    NSString *digitsOnly = @"";
    char c;
    for (int i = 0; i < cardNumber.length; i++){
        c = [cardNumber characterAtIndex:i];
        if (isdigit(c)){
            digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
        }
    }
    int sum = 0;
    int digit = 0;
    int addend = 0;
    BOOL timesTwo = false;
    for (NSInteger i = digitsOnly.length - 1; i >= 0; i--){
        digit = [digitsOnly characterAtIndex:i] - '0';
        if (timesTwo){
            addend = digit * 2;
            if (addend > 9) {
                addend -= 9;
            }
        }
        else {
            addend = digit;
        }
        sum += addend;
        timesTwo = !timesTwo;
    }
    int modulus = sum % 10;
    return modulus == 0;
}



/**
 详细地址  汉字数字横线
 
 @param str <#str description#>
 @return <#return value description#>
 */
+(BOOL)getDetailAddress:(NSString *)str{
    
    NSString *strRegex = @"[A-Za-z0-9_()\\-\\u4e00-\\u9fa5]{1,50}";
    return [CJRegular getResultByRegex:strRegex content:str];

}

//^[\u4E00-\u9FA5A-Za-z0-9_]+$
//+ (BOOL)getHomeAdress:(NSString *)str{
//
//    NSString *strRegex = @"[A-Za-z0-9_()\\-\\u4e00-\\u9fa5]{1,50}";
//
//    //    NSString *strRegex = @"[\u4e00-\u9fa5_a-zA-Z0-9_]{1,50}";
//
//    NSPredicate *QQTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",strRegex];
//
//    return [QQTest evaluateWithObject:str];
//}

/**
 信用码
 
 @param str <#str description#>
 @return <#return value description#>
 */

///  信用码
/// @param str <#str description#>
/// @param length 长度
+ (BOOL)getXYM:(NSString *)str length:(NSInteger )length{
    
//    NSString *strRegex = @"[A-Z0-9]{18,20}";
    NSString *strRegex = CJStringWithFormat(@"[a-zA-Z0-9]{1,%ld}",length);

    return [CJRegular getResultByRegex:strRegex content:str];
//    if (str.length == length) {
//        return YES;
//    }else{
//        return NO;
//    }

}


/// 正则校验金额，一个小数点，小数点后精确两位 最大10位
/// @param str <#str description#>
+ (BOOL)getIsMonayValue:(NSString *)str{
    
    NSString *strRegex = @"(\\+|\\-)?(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,17}(([.]\\d{0,2})?)))?";
    //@"(^[1-9](\\d+)?(\\.\\d{1,2})?$)|(^(0){1}$)|(^\\d\\.\\d{1,2}?$)";
    return [CJRegular getResultByRegex:strRegex content:str];
}

/// 正则校验金额，非负 ，一个小数点，小数点后精确两位
/// @param str <#str description#>
/// @param length 长度
+ (BOOL)getIsMonayValue:(NSString *)str length:(NSInteger )length{
    
    NSString *strRegex = CJStringWithFormat(@"(\\+|\\-)?(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,%ld}(([.]\\d{0,2})?)))?",(long)length);
    return [CJRegular getResultByRegex:strRegex content:str];
}


/// 正则校验金额，非负 ，一个小数点，小数点后精确三位
/// @param str <#str description#>
/// @param length <#length description#>
+ (BOOL)getIsMonayValueThreeDecimalPlaces:(NSString *)str length:(NSInteger )length{
    
    NSString *strRegex = CJStringWithFormat(@"(\\+|\\-)?(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,%ld}(([.]\\d{0,3})?)))?",(long)length);
    return [CJRegular getResultByRegex:strRegex content:str];
}


///车数要求 大于0的纯数字
/// @param str <#str description#>
/// @param length 数字长度
+ (BOOL)getCarNumsRequir:(NSString *)str length:(NSInteger )length{
    
//    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
//    NSString *strRegex = CJStringWithFormat(@"[0-9]{1,%ld}",(long)length);
//    return [CJRegular getResultByRegex:strRegex content:str];
    
    if (str.length == 1 && str.intValue > 0) {
        return YES;
    }else if(str.length > 1 && str.length <= length && str.intValue > 0){
        return YES;
    }else{
        return NO;
    }
}

//NSDictionary *dic = responseBody[@"Head"];
//
//if (!IsNilOrNull(dic)) {
//    [ws.view showImageHUDText:[NSString stringWithFormat:@"%@",dic[@"msg"]]];
//}else{
//    NSLog(@"注册失败");
//}



//- (void)alertControllerTitle:(NSString *)str{
//    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//    [alert addAction:action];
//    [self presentViewController:alert animated:YES completion:nil];
//    
//}

+(BOOL)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    BOOL ci;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *dt1 ;// = [[NSDate alloc] init];
//    NSDate *dt2 ; // = [[NSDate alloc] init];
    NSDate *dt1 = [df dateFromString:date01];
    NSDate *dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
        //date02比date01大
        case NSOrderedAscending: ci=YES; break;
        //date02比date01小
        case NSOrderedDescending: ci=NO; break;
        //date02=date01
        case NSOrderedSame: ci=YES; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
    return ci;
}


+ (BOOL)getFieldIsEmpty:(UITextField *)field{
    
    if (IsStrEmpty(field.text) || [[CJControl getThreeUnaryValueStr:field.text] isEqualToString:@""]) {
        return YES;
    }else{
        return NO;
    }
}

+ (BOOL)getIsStrEmpty:(NSString *)str{
    
    if (IsStrEmpty(str)) {
        return YES;
    }else{
        return NO;
    }
}

@end
